package br.com.amadeus.android.adapters;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import br.com.amadeus.android.activities.home.R;
import br.com.amadeus.android.entities.SocialHistory;
import br.com.amadeus.android.util.ConstantsAmadeusDroid;

public class SocialHistoryAdapter extends BaseAdapter {
	protected static final String CATEGORIA = "livro";
	private Context context;
	List<SocialHistory> listHistoric;
	private ImageView ivew;

	public SocialHistoryAdapter(Context context,
			List<SocialHistory> listHistoric) {
		super();
		this.context = context;
		Collections.reverse(listHistoric);
		this.listHistoric = listHistoric;
	}
	
	public SocialHistoryAdapter(Context context,
			List<SocialHistory> listHistoric, ImageView ivew) {
		super();
		this.context = context;
		Collections.reverse(listHistoric);
		this.listHistoric = listHistoric;
		this.ivew = ivew;
	}

	public int getCount() {
		return this.listHistoric.size();
	}

	public Object getItem(int posicao) {
		SocialHistory obj = this.listHistoric.get(posicao);
		return obj;
	}

	public long getItemId(int posicao) {
		return posicao;
	}

	public View getView(int posicao, View v, ViewGroup vg) {
		
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View view = inflater.inflate(R.layout.amadeus_social_history, null);
		
		SocialHistory sh = this.listHistoric.get(posicao);
		ImageView iv = ivew;
		
		try {
			final Bitmap imagem = BitmapFactory.decodeFile("/sdcard" + ConstantsAmadeusDroid.ROOT_ARCHIVES + "/" + sh.getUserName() + ".png");
			if (imagem != null) {
				iv.setImageBitmap(imagem);
			}
		} catch (Exception e) {
			Log.e(CATEGORIA, "Foto não encontrada.");
		}
		
		TextView tv = (TextView) view.findViewById(R.id.tvActionHistory);
		StringBuilder sb = new StringBuilder();
		SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
		sb.append(sh.getUserName() + " planejou " + sh.getActionValue() + " " + sh.getResourceName() + " - criado em " + format.format(sh.getCreationDate()));
		tv.setText(sb.toString());
		
		
		return view;
	}
		
}
